package cn.tedu.coolshark.Controller;

import cn.tedu.coolshark.mapper.BannerMapper;
import cn.tedu.coolshark.pojo.entity.Banner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    BannerMapper mapper;

    //处理首页轮播图展示的请求
    @RequestMapping("/select")
    public List<Banner> select() {
        return mapper.select();
    }

    //处理删除轮播图的请求
    @RequestMapping("/delete")
    public void delete(int id) {
        mapper.deleteById(id);
    }

    //处理添加轮播图的请求
    @RequestMapping("/insert")
    public void insert(String url) {
        mapper.insert(url);
    }

    //处理通过地址栏中的轮播图id查询轮播图详情的请求
    @RequestMapping("/selectById")
    public Banner selectById(int id) {
        return mapper.selectById(id);
    }

    @Value("${imagesPath}")
    private String dirPath;

    //处理修改轮播图的请求
    @RequestMapping("/update")
    public void update(@RequestBody Banner banner) {
        String url = mapper.selectUrlById(banner.getId()); //得到原来图片的路径
        new File(dirPath + url).delete(); //删除原来的图片
        mapper.update(banner); //修改
    }
}
